<?php
$prefix = "";
if (isset($_GET['ajax'])) {
    session_start();
    $prefix = "../";
}

include_once $prefix . 'controller/registrasi.php';
include_once $prefix . 'controller/login.php';
include_once $prefix . 'controller/follow.php';
include_once $prefix . 'controller/admin.php';
?>
<?php
if (isset($_GET['name_viewed'])!= NULL) {
    $name_viewed = $_GET['name_viewed'];
}
else if (isset($_SESSION['user_name'])!= NULL) {
    $name_viewed = $_SESSION['user_name'];
}
if (isset($_SESSION['user_name'])!= NULL) {
    $name_logon = $_SESSION['user_name'];
    $temp = new login();
    $role = $temp->getRole($name_logon );
}
else {
    $name_logon = NULL;
    $role = NULL;
}
?>
<?php
if(isset ($_POST['submitButton'])) {
// asumsikan semua sudah dicek pakai js
    $lontong = new registrasi();
    $lontong->register($_POST['username'], $_POST['nama'], $_POST['password'], $_POST['tanggalLahir'], $_POST['lokasi'], $_POST['jenisKelamin'], $_POST['email'], $_FILES['fotoProfil']);
}

else if (isset ($_POST['submitButtonLogin'])) {
    $lontong = new login();
    $lontong->login($_POST['usernameLogin'], $_POST['passwordLogin']);
}

?>
<table class="tabel">
    <tr>
        <th>
            <div>
                <form class="search" action="searchuser.php" method="post">
                    <label>User Name:</label>
                    <input type="text" name="search_user" id="search_user"/>
                    <input type="submit" value="Search User" id="search_user_button" name="search_user_button"/>
                </form>
            </div>
        </th>
        <th>
            <div>
                <form class="search" action="searchphoto.php" method="post">
                    <label>Photo:</label>
                    <input type="text" name="search_photo" id="search_photo"/>
                    <input type="submit" value="Search Photo" id="search_photo_button" name="search_photo_button"/>
                </form>
            </div>
        </th>
    </tr>
</table>
<table class="tabel2" id="panjang">
    <tr>
        <th class="grid1">
            <div>
                <div>
                    <label id="titlebold">Recent Registered Users</label>
                </div>
                <div class="space"></div><div class="space"></div>
                <?php
                $user = new registrasi();
                $resultUser = $user->showLastRegistered();
                $iterate=0;
                while($row = mysql_fetch_array($resultUser)) {
                    ?>
                <div class="floatleft" id="photogalleryhome">
                        <?php
                        $urlPict = $row['url_photo'];
                        $urlProfile = "profil.php?name_viewed=" . $row['user_name'];
                        $iterate ++;
                        if ($iterate == 6) {
                            break;
                        }
                        ?>
                    <a href="<?php echo $urlProfile ?>" >
                        <img id="<?php echo "reguser_" . $iterate?>" name="<?php echo "reguser_" . $iterate?>" src="<?php echo $urlPict ?>" alt="<?php echo "reguser_" . $iterate?>" width="80" height="80" border="0"/>
                    </a>

                </div>
                    <?php } ?>
            </div>
        </th>
        <th class="grid2">
            <div>
                <div>
                    <label id="titlebold">Recent Uploaded Photos</label>
                </div>
                <div class="space"></div><div class="space"></div>
                <?php
                $photo = new photo();
                $resultPhoto = $photo->showLastUploadedPhoto();
                $iterate=0;
                while($row = mysql_fetch_array($resultPhoto)) {
                    $fol = new follow();
                    if ($row['photo_prev'] == 'login_user') {
                        if ($name_logon == NULL) {
                            continue;
                        }
                    }else if ($row['photo_prev'] == 'owner') {
                        if ($name_logon != $row['user_name']) {
                            continue;
                        }
                    }else if ($row['photo_prev'] == 'follower_user') {
                        $follow = $fol->isFollowing($row['user_name'], $name_logon);
                        if ($name_logon == $row['user_name']) {

                        }else if (!$follow) {
                            continue;
                        }
                    }
                    ?>
                <div class="floatleft" id="photogalleryhome">
                        <?php
                        $urlPict = $row['url_photo'];
                        $urlPhoto = "foto_zoom.php?name_viewed=" . $row['user_name'];
                        $iterate ++;
                        if ($iterate == 6) {
                            break;
                        }
                        ?>
                    <a href="<?php echo $urlPhoto.'&url_foto='.$row['url_photo'] ?>" >
                        <img id="<?php echo "upphoto_" . $iterate?>" name="<?php echo "upphoto_" . $iterate?>" src="<?php echo $urlPict ?>" alt="<?php echo "upphoto_" . $iterate?>" width="80" height="80" border="0"/>
                    </a>

                </div>
                    <?php } ?>
            </div>
        </th>
    </tr>
    <tr>
        <th class="grid3">
            <div>
                <div>
                    <label id="titlebold">Best Valued Photos</label>
                </div>
                <div class="space"></div><div class="space"></div>
                <?php
                $photo = new photo();
                $resultPhoto = $photo->showMostValuePhoto();
                $iterate=0;
                while($row = mysql_fetch_array($resultPhoto)) {
                    $fol = new follow();
                    if ($row['photo_prev'] == 'login_user') {
                        if ($name_logon == NULL) {
                            continue;
                        }
                    }else if ($row['photo_prev'] == 'owner') {
                        if ($name_logon != $row['user_name']) {
                            continue;
                        }
                    }else if ($row['photo_prev'] == 'follower_user') {
                        $follow = $fol->isFollowing($row['user_name'], $name_logon);
                        if ($name_logon == $row['user_name']) {

                        }else if (!$follow) {
                            continue;
                        }
                    }
                    ?>
                <div class="floatleft" id="photogalleryhome">
                        <?php
                        $urlPict = $row['url_photo'];
                        $urlPhoto = "foto_zoom.php?name_viewed=" . $row['user_name'] . "&url_foto=" .$row['url_foto'];
                        $iterate ++;
                        if ($iterate == 6) {
                            break;
                        }
                        ?>
                    <a href="<?php echo $urlPhoto.'&url_foto='.$row['url_photo'] ?>" >
                        <img id="<?php echo "upphoto_" . $iterate?>" name="<?php echo "upphoto_" . $iterate?>" src="<?php echo $urlPict ?>" alt="<?php echo "upphoto_" . $iterate?>" width="80" height="80" border="0"/>
                    </a>

                </div>
                    <?php } ?>
            </div>
        </th>
        <th class="grid4">
            <div>
                <div>
                    <label id="titlebold">Most Popular Photos</label>
                </div>
                <div class="space"></div><div class="space"></div>
                <?php
                $photo = new photo();
                $resultPhoto = $photo->showMostPopularPhoto();
                $iterate=0;
                while($row = mysql_fetch_array($resultPhoto)) {
                    $fol = new follow();
                    if ($row['photo_prev'] == 'login_user') {
                        if ($name_logon == NULL) {
                            continue;
                        }
                    }else if ($row['photo_prev'] == 'owner') {
                        if ($name_logon != $row['user_name']) {
                            continue;
                        }
                    }else if ($row['photo_prev'] == 'follower_user') {
                        $follow = $fol->isFollowing($row['user_name'], $name_logon);
                        if ($name_logon == $row['user_name']) {

                        }else if (!$follow) {
                            continue;
                        }
                    }
                    ?>
                <div class="floatleft" id="photogalleryhome">
                        <?php
                        $urlPict = $row['url_photo'];
                        $urlPhoto = "foto_zoom.php?name_viewed=" . $row['user_name'] . "&url_foto=" .$row['url_foto'];
                        $iterate ++;
                        if ($iterate == 6) {
                            break;
                        }
                        ?>
                    <a href="<?php echo $urlPhoto.'&url_foto='.$row['url_photo'] ?>" >
                        <img id="<?php echo "upphoto_" . $iterate?>" name="<?php echo "upphoto_" . $iterate?>" src="<?php echo $urlPict ?>" alt="<?php echo "upphoto_" . $iterate?>" width="80" height="80" border="0"/>
                    </a>

                </div>
                    <?php } ?>
            </div>
        </th>
    </tr>
</table>